﻿using System;
using System.IO;
using AudioCore.Common;

namespace AudioBookWS
{
    public partial class AudioFile : System.Web.UI.Page
    {
        //http://localhost/audiows/audiofile.aspx?u={0}&a={1}&p={2}
        //http://localhost/audiows/audiofile.aspx?u=1&a=6&p=223

        /// <summary>
        /// Physical path of audio data folder on the server
        /// </summary>

        private const string USER_ID_PARAM = "u";
        private const string ARTICLE_ID_PARAM = "a";
        private const string ARTICLE_PART_ID_PARAM = "p";
        private const string BLOCK_NUMBER_PARAM = "b";

        private const string FILE_NOT_FOUND_MSG = "File not found.";
        private const string PARAM_INVALID = "Parameters invalid.";

        protected void Page_Load(object sender, EventArgs e)
        {
            int userID, articleID, partID, blockNumber;

            if (Request.QueryString[USER_ID_PARAM] != null
                && int.TryParse(Request.QueryString[USER_ID_PARAM].ToString(), out userID))
            {
                if (Request.QueryString[ARTICLE_ID_PARAM] != null
                    && int.TryParse(Request.QueryString[ARTICLE_ID_PARAM].ToString(), out articleID))
                {
                    if (Request.QueryString[ARTICLE_PART_ID_PARAM] != null)
                    {
                        if (int.TryParse(Request.QueryString[ARTICLE_PART_ID_PARAM].ToString(), out partID))
                        {
                            WriteAudio(userID, articleID, partID);
                        }
                        else
                        {
                            WriteError(PARAM_INVALID);
                        }
                    }
                    else
                    {
                        WriteAudio(userID, articleID);
                    }
                }
                else
                {
                    WriteError(PARAM_INVALID);
                }
            }
            else
            {
                WriteError(PARAM_INVALID);
            }
        }

        /// <summary>
        /// Get all audio files of an article and then write down to the client
        /// </summary>
        /// <param name="userID">UserID</param>
        /// <param name="articleID">ArticleID</param>
        private void WriteAudio(int userID, int articleID)
        {
            //Will be developed later
        }

        /// <summary>
        /// Get original audio file of an article and then write down to the client
        /// </summary>
        /// <param name="userID">UserID</param>
        /// <param name="articleID">Article id</param>
        /// <param name="partID">Article part id</param>
        private void WriteAudio(int userID, int articleID, int partID)
        {
            string audioFile = GlobalParam.DataFolder + Constant.AUDIO_FOLDER + articleID.ToString() + "\\" +
                partID.ToString() + "_000" + Constant.AUDIO_EXTENSION;

            WriteFile(audioFile);
        }

        /// <summary>
        /// Write a block of audio file to the client
        /// </summary>
        /// <param name="userID">User ID</param>
        /// <param name="articleID">Article ID</param>
        /// <param name="partID">Article part ID</param>
        /// <param name="blockNum">Order of block</param>
        private void WriteAudio(int userID, int articleID, int partID, int blockNum)
        {
            string blockName;
            if (blockNum == -1)
            {
                blockName = "_org";
            }
            else
            {
                blockName = "000" + blockNum.ToString();
                blockName = "_" + blockName.Substring(blockName.Length - 3);
            }

            string audioFile = GlobalParam.DataFolder + Constant.AUDIO_FOLDER + articleID.ToString() + "\\" +
               partID.ToString() + blockName + Constant.AUDIO_EXTENSION;

            WriteFile(audioFile);
        }

        /// <summary>
        /// Write down the content of a file to the client
        /// </summary>
        /// <param name="filePath">Physical path of the file</param>
        private void WriteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                string fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1);

                Response.Clear();
                Response.ContentType = "audio/mpeg3";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                Response.WriteFile(filePath);
                Response.Flush();
            }
            else
            {
                WriteError(FILE_NOT_FOUND_MSG);
            }
        }

        private void WriteError(string message)
        {
            Response.Clear();
            Response.Write(message);
        }
    }
}
